package com.westcom.healthmate.common.constants;

import org.apache.http.HttpStatus;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ErrorConstants
{
    /////////////////////////////////////////////////////////////////////////////////////////
    @OperationStatusAnn(httpStatus = HttpStatus.SC_OK,msg = "success")
    public static String SUCCESS = "0";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR,msg = "interval error")
    public static String INTERVAL_ERROR = "1001";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "not found")
    public static String NOT_FOUND = "1002";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "third part info not found")
    public static String THIRD_PART_INFO_NOT_FOUND = "1003";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "mira sleep data not found")
    public static String MIRA_SLEEP_DATA_NOT_FOUND = "1004";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "mira daily report not found")
    public static String MIRA_SLEEP_DAILY_REPORT_NOT_FOUND = "1005";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "mira daily evaluate not found")
    public static String MIRA_SLEEP_DAILY_EVALUATE_NOT_FOUND = "1006";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "mac is missing")
    public static String MAC_NOT_FOUND = "1007";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR,msg = "IO error")
    public static String IO_ERROR = "1008";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_UNAUTHORIZED,msg = "mira ak sk auth failed")
    public static String MIRA_AUTH_FAILED = "2001";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "specify frameware not found")
    public static String FRAMEWARE_NOT_FOUND = "2011";

    @OperationStatusAnn(httpStatus = HttpStatus.SC_NOT_FOUND,msg = "frameware operation error")
    public static String FRAMEWARE_OPERATION_FAILED= "2011";

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface OperationStatusAnn
    {
        public int httpStatus() default 200;
        public String msg() default "";

    }
    public static class OperationStatus
    {
        private int httpStatus;
        private String msg;
        public OperationStatus(int httpStatus,String msg)
        {
            this.httpStatus = httpStatus;
            this.msg = msg;
        }

        public int getHttpStatus() {
            return httpStatus;
        }

        public void setHttpStatus(int httpStatus) {
            this.httpStatus = httpStatus;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    public static Map<String,OperationStatus> OPERATION_STATUS = new HashMap<>();

    static
    {
        ErrorConstants ec = new ErrorConstants();
        Field[] fs = ErrorConstants.class.getDeclaredFields();
        for(Field f:fs)
        {
            OperationStatusAnn ann = f.getAnnotation(OperationStatusAnn.class);
            if(null != ann)
            {
                try
                {
                    f.setAccessible(true);
                    String code = f.get(ec.getClass()).toString();
                    OPERATION_STATUS.put(code,new OperationStatus(ann.httpStatus(),ann.msg()));
                } catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

}
